package com.pfzy.utildemo

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.pfzy.traveldriver3.ActivityAdapter
import com.pfzy.traveldriver3.DemoDetails
import com.pfzy.utildemo.activity.db.DBActivity
import com.pfzy.utildemo.activity.im.IMActivity
import com.pfzy.utildemo.activity.local.LocalActivity
import com.pfzy.utildemo.activity.mqtt.MqttActivity
import com.pfzy.utildemo.activity.record.RecordActivity
import com.pfzy.utildemo.activity.tts.TTSActivity
import com.pfzy.utildemo.databinding.ActivityMainBinding
import java.util.ArrayList

class MainActivity : AppCompatActivity() {
    private lateinit var mBind: ActivityMainBinding
    private var mAdapter: ActivityAdapter? = null
    private val mDemoList: MutableList<DemoDetails> = ArrayList<DemoDetails>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBind.root)
        initView()
    }

    private fun initView() {
        initDemoList()

        mBind.mainList.layoutManager = LinearLayoutManager(this)
        mAdapter = ActivityAdapter(mDemoList)
        mBind.mainList.adapter = mAdapter
        mAdapter!!.setOnItemClickListener { adapter, view, position ->
            val demo: DemoDetails = adapter.getItem(position) as DemoDetails
            if (demo.activityClass != null) {
                val intent = Intent()
                intent.setClass(this@MainActivity, demo.activityClass)
                startActivity(intent)
            }
        }
    }

    private fun initDemoList() {
        mDemoList.add(DemoDetails("录音", RecordActivity::class.java))
        mDemoList.add(DemoDetails("TTS", TTSActivity::class.java))
        mDemoList.add(DemoDetails("MQTT", MqttActivity::class.java))
        mDemoList.add(DemoDetails("即时通讯", IMActivity::class.java))
        mDemoList.add(DemoDetails("定位", LocalActivity::class.java))
        mDemoList.add(DemoDetails("数据库", DBActivity::class.java))
    }
}